Contents | Index | < Browse | Browse >
LETTERatofULETTER
Converts an ASCII string to a floating-point number.
Overview
#include <stdlib.h>
x = atof(s);
double x; // floating-point result
const char *s; // source string pointer
Portability
ANSI
Description
This function converts an ASCII string to a double-precision floating-point
number. Calling "atof" equals "strtod(string, 0)".
The input string is assumed to have the following format:
[ws][sign]digits[.][digits][e|E[sign]digits]
where ws stands for any number of whitespaces (plain spaces and
tabs), sign is either '+' or '-' and digits means
a sequence of numbers between 0 and 9. All elements within "[]" brackets are
optional. The following string is valid for atof:
+123.456e-78
Returns
This function returns the double-precision equivalent of the ASCII string
passed to it. If an overflow occurs it will return plus or minus HUGE_VAL and
errno will contain ERANGE.
See also
atoi , atol , strtod
Example
#include <stdio.h>
#include <math.h>
void main(void)
{
char buff[80];
double d;
while(1)
{
printf("\nPlease enter a number: ");
if(fgets(buff, sizeof(buff), stdin) == NULL)
{
break;
}
if(buff[0] == '\0')
{
break;
}
d = atof(buff);
printf("%e\n",d);
}
printf("\n");
}